package com.hippo.core.ui.components

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountBox
import androidx.compose.material.icons.filled.Email
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.google.android.material.tabs.TabItem
import com.hippo.core.R
import com.hippo.core.core.ext.click
import com.hippo.core.ui.theme.HippoTheme


@Composable
fun BottomTabBar(
    modifier: Modifier = Modifier,
    checkIndex: Int,
    onItemChecked: (Int) -> Unit
) {


    Column {

        HorizontalDivider(
            thickness = 1.dp,
            color = Color(0xFFE6E7EB)
        )


        Row(
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    color = Color(0xFFFFFFFF)
                )
                .padding(vertical = 10.dp)
        ) {
            TabItem(
                modifier = modifier.weight(1F),
                checked = checkIndex == 0,
                name = stringResource(R.string.main_1),
                checkedPainter = painterResource(R.mipmap.home_s),
                unCheckedPainter = painterResource(R.mipmap.home_n),
                onClick = {
                    onItemChecked(0)
                }
            )
            TabItem(
                modifier = modifier.weight(1F),
                checked = checkIndex == 1,
                name = stringResource(R.string.main_2),
                checkedPainter = painterResource(R.mipmap.spt_s),
                unCheckedPainter = painterResource(R.mipmap.spt_n),
                onClick = {
                    onItemChecked(1)
                }
            )
            TabItem(
                modifier = modifier.weight(1F),
                checked = checkIndex == 2,
                name = stringResource(R.string.main_3),
                checkedPainter = painterResource(R.mipmap.me_s),
                unCheckedPainter = painterResource(R.mipmap.me_n),
                onClick = {
                    onItemChecked(2)
                }
            )

        }
    }
}


@Composable
private fun TabItem(
    modifier: Modifier = Modifier,
    name: String,
    checkedPainter: Painter,
    unCheckedPainter: Painter,
    checked: Boolean = false,
    onClick: () -> Unit
) {
    Column(
        modifier = modifier.click { onClick() },
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        var tabColor = if (checked) MaterialTheme.colorScheme.primary else Color.Black
        var tabPainter = if (checked) checkedPainter else unCheckedPainter


        Image(
            painter = tabPainter,
            contentDescription = name,
            modifier = Modifier.size(18.dp)
        )
        Text(
            text = name,
            color = tabColor,
            style = MaterialTheme.typography.labelSmall,
            modifier = Modifier
                .padding(top = 5.dp)
                .align(Alignment.CenterHorizontally)
        )
    }
}


@Preview(showBackground = true, device = Devices.PHONE)
@Composable
private fun BottomTabBarPreview() {
    HippoTheme {
        BottomTabBar(
            checkIndex = 0,
            onItemChecked = {}
        )
    }

}